package com.loctek.importexport.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.gyf.barlibrary.ImmersionBar;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.loctek.importexport.R;
import com.loctek.importexport.util.MenuColorizer;

public abstract class BaseActionBarActivity extends AppCompatActivity {


    private boolean isSpecialActionbar;

    private ProgressBar mProgressBar;

    KProgressHUD progressHUD;

    protected ImmersionBar mImmersionBar;

    private boolean isOverlayActionBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(isOverlayActionBar ? R.layout.toolbar_acitvity_overlay_holder : R.layout.toolbar_acitvity_holder);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (isSpecialActionbar) {
            mProgressBar = (ProgressBar) findViewById(R.id.progress_spinner);
        }
        setUpToolBar();

        progressHUD = KProgressHUD.create(BaseActionBarActivity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在加载中")
                .setDetailsLabel("请稍等...")
                .setCancellable(false)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f);
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarDarkFont(true, 0.2f).init();   //所有子类都将继承这些相同的属性

    }


    public void showLoading() {
        if (!progressHUD.isShowing()) {
            progressHUD.show();
        }
    }

    public void dismissLoading() {
        if (progressHUD.isShowing()) {
            progressHUD.dismiss();
        }
    }


    private void setUpToolBar() {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
    }


    @Override
    public void setContentView(int layoutResID) {
        ((FrameLayout) findViewById(R.id.view_holder)).removeAllViews();
        LayoutInflater.from(this).inflate(layoutResID, (FrameLayout) findViewById(R.id.view_holder));

    }

    @Override
    public void setContentView(View view) {
        ((FrameLayout) findViewById(R.id.view_holder)).removeAllViews();
        ((FrameLayout) findViewById(R.id.view_holder)).addView(view);
    }


    public void setProgressVisible(boolean visible) {
        if (mProgressBar != null) {
            mProgressBar.setVisibility(visible ? View.VISIBLE : View.GONE);
        } else {
            if (visible) {
                throw new IllegalArgumentException("may not execute enableProgress() ");
            } else {
                View view = findViewById(R.id.progress_spinner);
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissLoading();
        if (mImmersionBar != null)
            mImmersionBar.destroy();  //必须调用该方法，防止内存泄漏，不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
    }


    protected void enableProgress() {
        isSpecialActionbar = true;
    }

    public void setOverlayActionBar(boolean overlayActionBar) {
        isOverlayActionBar = overlayActionBar;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //需要在activtiy中创建menu
        MenuColorizer.colorMenu(this, menu, R.color.theme_main_color);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * 设置分割线是否显示,需要全屏显示的时候需要设置,否则会有白边
     *
     * @param visible
     */
    protected void setDivideLineVisible(boolean visible) {
        View divideNormal = findViewById(R.id.toolbar_divide_line_normal);
        if (divideNormal != null) {
            divideNormal.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
        View divideDark = findViewById(R.id.toolbar_divide_line_dark);
        if (divideDark != null) {
            divideDark.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }

    protected void setBackgroundColor(int colorRes) {
        View view = findViewById(R.id.base_toolbar_activity_holder);
        if (view != null) {
            view.setBackgroundResource(colorRes);
        }
    }


}
